package my.service.Impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import my.utils.DBUtils;
import my.bean.Goods;
import my.dao.IGoodDao;
import my.dao.Impl.IGoodsDaoImpl;
import my.service.IGoodsService;

/**
 * 业务实现类,通过调用数据访问层代码,实现数据处理功能
 * 
 * @author slim
 *
 */
public class IGoodsServiceImpl implements IGoodsService {

	private IGoodDao goodsDao;

	@Override
	public int add(Goods good) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			goodsDao = new IGoodsDaoImpl(conn);
			record = goodsDao.add(good);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("发生错误!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public void addGoodList(List<Goods> goods) throws SQLException {
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			goodsDao = new IGoodsDaoImpl(conn);
			goodsDao.addGoodList(goods);
			conn.commit();
		} catch (SQLException e) {
			System.out.println("发生错误!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int deleteByGoodId(int gid) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			goodsDao = new IGoodsDaoImpl(conn);

			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("发生错误!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int modify(Goods good) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			goodsDao = new IGoodsDaoImpl(conn);

			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("发生错误!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Goods> findAll() {
		Connection conn = null;
		List<Goods> list = null;

		try {
			conn = DBUtils.getConnection();
			goodsDao = new IGoodsDaoImpl(conn);
			list = goodsDao.findAll();
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Goods> findByType(int type) {
		Connection conn = null;
		List<Goods> list = null;

		try {
			conn = DBUtils.getConnection();
			goodsDao = new IGoodsDaoImpl(conn);
			list = goodsDao.findByType(type);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Goods findByGoodId(int gid) {
		Connection conn = null;
		Goods good = null;

		try {
			conn = DBUtils.getConnection();
			goodsDao = new IGoodsDaoImpl(conn);
			good = goodsDao.findByGoodId(gid);
			return good;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

}
